<template>
	<view class="">
		<u-sticky>
			<view class="" style="padding: 0 28rpx;">
				<homeHeader></homeHeader>
				<u-tabs :list="tabList" activeStyle="font-weight:bold;color:#000000;"
					inactiveStyle="color:#9C9C9C;font-weight:nomal;" lineHeight="3px" lineWidth="20px"
					@change="tabsChange"></u-tabs>
			</view>

		</u-sticky>
		<u-empty mode="data" width="300rpx" height="300rpx" iconSize="160rpx" textSize="20rpx" marginTop="100rpx" v-if="data.length == 0">
		</u-empty>
		<template v-for="(item,index) of data">
			<!-- 工作 -->
			<jobItemComponent :info="item" v-if="item.task_type == 'task_job' || item.task_type == 'task_medical'">
			</jobItemComponent>
			<!-- 住房 -->
			<houseItemComponent :info="item" v-if="item.task_type == 'task_house'"></houseItemComponent>
		</template>
		<view class="bottom-btn" @click="toSubmit">
			去发布
		</view>
		<popLoginComponet ref="login" :shouldAuthor="true" @success="getData"></popLoginComponet>
		<u-picker :show="show" :columns="columns" title="选择发布类型" keyName="label" :closeOnClickOverlay="true" @cancel="closeHandler" @confirm="confirmHandler"></u-picker>
	</view>
</template>

<script>
	import homeHeader from "@/components/home_header/index.vue"
	import jobItemComponent from "@/components/jobItemComponent/index.vue"
	import houseItemComponent from "@/components/houseItemComponent/index.vue"
	import popLoginComponet from "@/components/popLoginComponent/index.vue"
	
	import {
		getTaskMinePage
	} from "@/api/task.js"
	export default {
		components: {
			homeHeader,
			jobItemComponent,
			houseItemComponent,
			popLoginComponet
		},
		data() {
			return {
				tabList: [{
					name: '已发布',
					audit_status: 1,
					status: 0
				}, {
					name: '审核中',
					audit_status: 0,
					status: 0
				}, {
					name: '审核失败',
					audit_status: 2,
					status: 0
				}, {
					name: '已下架',
					status: 1
				}],
				page: {
					page_size: 7,
					page_number: 1,
					total: 0
				},
				data: [],
				load_status: "loadmore",
				audit_status: 1,
				status: 0,
				show: false,
				columns: [[{
					label: '发布工作',
					value: 'task_job'
				},{
					label: '发布住房',
					value: 'task_house'
				},{
					label: '发布医疗',
					value: 'task_medical'
				}]]
			}
		},
		onShow() {
			this.page = {
				page_size: 7,
				page_number: 1,
				total: 0
			}
			this.data = []
			this.load_status = "loadmore"
			this.getData()
		},
		methods: {
			closeHandler(){
				this.show = false
			},
			confirmHandler(e){
				const type = (e.value[0] || {}).value
				if(type == 'task_job'){
					uni.navigateTo({
						url: "/pages/publish_job/index"
					})
				}
				if(type == "task_medical"){
					uni.navigateTo({
						url: "/pages/publish_medical/index"
					})
				}
				if(type == 'task_house'){
					uni.navigateTo({
						url: '/pages/publish_house/index'
					})
				}
				this.show = false
			},
			validateLogin() {
				return this.$refs.login.validaLogin()
			},
			async toSubmit(){
				await this.validateLogin()
				this.show = true
			},
			tabsChange(e) {
				this.audit_status = e.audit_status
				this.status = e.status
				this.page = {
					page_size: 7,
					page_number: 1,
					total: 0
				}
				this.data = []
				this.load_status = 'loadmore'
				this.getData()
			},
			getData() {
				if(!this.$refs.login.isLogin()){
					return
				}
				if (this.load_status != "nomore") {
					this.load_status = "loading"
				} else {
					return
				}
				const query = {}
				if (this.audit_status !== undefined) {
					query.audit_status = this.audit_status
				}
				if (this.status !== undefined) {
					query.status = this.status
				}
				getTaskMinePage({
						...this.page,
						...query
					})
					.then(res => {
						const data = (res.list || []).map(item => {
							try {
								item.images = JSON.parse(item.images)
							} catch (e) {
								item.images = []
							}
							try {
								item.tag = JSON.parse(item.tag)
							} catch (e) {
								item.tag = []
							}
							try{
								item.facilities = JSON.parse(item.facilities).slice(0,2)
							}catch(e){
								item.facilities = []
							}
							return item
						})
						this.page.total = res.total
						this.data.push(...data)
						if (this.data.length == res.total) {
							this.load_status = "nomore"
						} else {
							this.page.page_number += 1
							this.load_status = "loadmore"
						}
					})
					.catch(e => {
						this.load_status = "loadmore"
					})
			}

		}
	}
</script>

<style>
	page {
		background: #649CF6 linear-gradient(360deg, #fff 0%, #ECF3FF 60%, #F1F6FF 96%, #F1F6FF 100%);
		padding-bottom: 200rpx;
	}
</style>
<style scoped lang="scss">
	.bottom-btn{
		position: absolute;
		bottom: 100rpx;
		left: 0;
		right: 0;
		margin: auto;
		width: 630rpx;
		line-height: 86rpx;
		background: #2B5BF8;
		border-radius: 86rpx;
		font-weight: 500;
		font-size: 38rpx;
		color: #FFFFFF;
		text-align: center;
	}
</style>